package com.sskj.pay.presenter;

import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.bean.PayTypeEnum;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.pay.bean.PayTypeBean;
import com.sskj.pay.bean.PayTypeResultBean;
import com.sskj.pay.http.HttpConfig;
import com.sskj.pay.ui.activity.PayTypeActivity;

import java.util.ArrayList;


public class PayTypeActivityPresenter extends BasePresenter<PayTypeActivity> {
    public void getData() {
        httpService.getPayType()
                .execute(new JsonCallBack<HttpData<PayTypeResultBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<PayTypeResultBean>> response) {

                        HttpData<PayTypeResultBean> data = response.body();
                        PayTypeResultBean.StatusBean status = data.getData().getStatus();
                        ArrayList<PayTypeBean> payTypeBeans = new ArrayList<>();

                        PayTypeBean payTypeBeanWX = new PayTypeBean(PayTypeEnum.WX);
                        if (status.getWx().getId() != 0) {
                            payTypeBeanWX.setAdd(true);
                            payTypeBeanWX.setOpen(status.getWx().getStatus() != 0);
                            payTypeBeanWX.setAccount(status.getWx().getNumber());
                            payTypeBeanWX.setName(status.getWx().getName());
                            payTypeBeanWX.setImg(status.getWx().getImg());
                        }
                        payTypeBeans.add(payTypeBeanWX);

                        PayTypeBean payTypeBeanAlipay = new PayTypeBean(PayTypeEnum.ALIPAY);
                        if (status.getAlipay().getId() != 0) {
                            payTypeBeanAlipay.setAdd(true);
                            payTypeBeanAlipay.setOpen(status.getAlipay().getStatus() != 0);
                            payTypeBeanAlipay.setAccount(status.getAlipay().getNumber());
                            payTypeBeanAlipay.setName(status.getAlipay().getName());
                            payTypeBeanAlipay.setImg(status.getAlipay().getImg());
                        }
                        payTypeBeans.add(payTypeBeanAlipay);

                        PayTypeBean payTypeBeanBank = new PayTypeBean(PayTypeEnum.BANK);
                        if (status.getBackcard().getId() != 0) {
                            payTypeBeanBank.setAdd(true);
                            payTypeBeanBank.setOpen(status.getBackcard().getStatus() != 0);
                            payTypeBeanBank.setAccount(status.getBackcard().getBank_car());
                            payTypeBeanBank.setName(status.getBackcard().getName());
                            payTypeBeanBank.setBank(status.getBackcard().getNumber());
                            payTypeBeanBank.setBranch(status.getBackcard().getBranch());
                        }
                        payTypeBeans.add(payTypeBeanBank);
                        mView.updateData(payTypeBeans);
                    }
                });
    }

    public void togglePayType(boolean isOpen, String type) {
        httpService.togglePayType(isOpen ? "1" : "0", type)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtil.showShort(httpData.getMsg());
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.toggleSuccess();
                        }
                    }
                });
    }
}
